﻿using DeviceCommons.DeviceMessages.Abstractions;
using DeviceCommons.DeviceMessages.Models.V1;
using System.Buffers;

namespace DeviceCommons.DeviceMessages.Serialization.V1.Serializers
{
    public class DeviceMessageInfoReadingsSerializer : AbstractMessageSerializer<IDeviceMessageInfoReadings>, IDeviceMessageInfoReadingsSerializer
    {
        public DeviceMessageInfoReadingsSerializer() : base(new DeviceMessageInfoReadings())
        {
        }

        public override void Serializer(ArrayBufferWriter<byte> writer, IDeviceMessageInfoReadings model)
        {
            ArgumentNullException.ThrowIfNull(model);

            SerializeWithHeader(
                writer,
                headerLength: 1,
                writeHeaderAction: w => w.Write([model?.Count ?? 0]),
                serializeContentAction: w =>
                {
                    foreach (var reading in model.ReadingArray ?? [])
                    {
                        new DeviceMessageInfoReadingSerializer().Serializer(writer, reading);
                    }
                });
        }
    }
}
